// goods/pages/goodsDetails/goodsDetails.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId: "",
    goodsInfo: null,
    goods_num: 1,
    toggle: false,
    collected: null,
    collectText: "",
  },
  // 显示弹窗
  onToggle() {
    const that = this;
    that.setData({
      toggle: true
    })
  },
  // 关闭弹窗
  onCloseToggle() {
    const that = this;
    that.setData({
      toggle: false
    })
  },
  // 回到首页
  toIndex() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  //进入购物车页面
  toCart() {
    wx.navigateTo({
      url: '/goods/pages/shoppingCart/shoppingCart',
    })
  },
  //加入购物车
  addCart() {
    const that = this;
    let data = {
      goods_num: 1,
      goods_id: that.data.goodsId,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.addCart, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000
        })
      } else {
        wx.showModal({
          showCancel: false,
          content: res.data.message,
          success(res) {
            if (res.confirm) {

            }
          }
        })
      }
    })
  },
  getData(id) {
    const that = this;
    let data = {
      id
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.goodsInfo, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        that.setData({
          goodsInfo: res.data.result,
          collectText: res.data.result.is_collect ? '已收藏' : "收藏",
          collected: res.data.result.is_collect ? true : false,
        })
      }
    })
  },
   //收藏
   collect(scope) {
    const that = this;
    let collected = that.data.collected
    let goods_id = scope.currentTarget.dataset.goods_id
    let data = {
      goods_id: goods_id,
    }
    wx.showLoading({
      title: '加载中...',
    })
    if (collected) {
      myRequest.EasyHttp.post(api.reqUrls.collectGoodsOrNo, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
          that.setData({
            collected: false,
            collectText: '收藏',
          
          })
        }
      })
    } else {
      myRequest.EasyHttp.post(api.reqUrls.collectGoodsOrNo, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
          that.setData({
            collectText: '已收藏',
            collected: true,
          })
        }
      })
    }

  },
  //立即购买数量更新
  myevents(e) {
    const that = this;
    that.setData({
      goods_num: e.detail.val
    })
  },
  //跳转到生成订单页
  toCreateOrder() {
    const that = this;
    wx.navigateTo({
      url: `/goods/pages/createOrder/createOrder?goodsId=${that.data.goodsId}&goodsNum=${that.data.goods_num}&account=buy_now`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.setData({
      goodsId: options.goodsId
    })
    that.getData(options.goodsId)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})